//
//  OAReadRecordController.m
//  OASystem
//
//  Created by zhoupengfei on 15/5/9.
//  Copyright (c) 2015年 zpf. All rights reserved.
//

#import "OAReadRecordController.h"
#import "HttpTools.h"
#import "GDataDefines.h"
#import "OAToolBox.h"
#import "MBProgressHUD.h"
@interface OAReadRecordController ()
@property(nonatomic,copy)NSString * readinfo;
@property (nonatomic,strong)NSArray *array;
@end

@implementation OAReadRecordController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"阅读记录";
    [self loadData];
}

-(void)loadData{
    [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray * array = @[@{@"unId":self.fileId},@{@"userId":userId}];
    [HttpTools postWithURL:KexchgeneralURL arrays:array method:KLookRecord success:^(id JSON) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        [self getDataFromXml:JSON];
    } failure:^(NSError *error) {
         [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
    }];
}

-(void)getDataFromXml:(id)json{
    NSData * data = [json dataUsingEncoding:NSUTF8StringEncoding];
    GDataXMLDocument *xmlDocument = [[GDataXMLDocument alloc] initWithData: data options:0 error:nil];
    //获取根节点
    GDataXMLElement *rootElement = [xmlDocument rootElement];
    //获取根节点下的节点
    NSArray *rootData= [rootElement elementsForName:@"数据"];
    for (GDataXMLElement *subElement in rootData) {
        NSArray *tempData = [subElement elementsForName:@"数据要素"];
        for (GDataXMLElement *data in tempData) {
            GDataXMLElement * fileNumElement = [[data elementsForName:@"readinfo"] objectAtIndex:0];
          self.readinfo = [fileNumElement stringValue];
        }
    }
    
    [self.tableView reloadData];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

 self.array = [self.readinfo componentsSeparatedByString:@","];
 return self.array.count;
}

#pragma mark UITbableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * cellIdentifier = @"cellIdentifier";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell ==nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    self.array = [self.readinfo componentsSeparatedByString:@","];
    cell.textLabel.text =self.array[indexPath.row];
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return  44;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
